import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react-swc';
import { resolve } from 'path';

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
    },
  },
  server: {
    host: '0.0.0.0',
    port: 5174,
    strictPort: true,
  },
  build: {
    target: 'es2015',
    minify: 'terser',
    rollupOptions: {
      output: {
        manualChunks: (id) => {
          // 将 node_modules 中的包分离到不同的 chunk
          if (id.includes('node_modules')) {
            if (id.includes('react') || id.includes('react-dom')) {
              return 'react-vendor';
            }
            if (id.includes('antd')) {
              return 'antd-vendor';
            }
            if (id.includes('@ant-design/icons')) {
              return 'antd-icons';
            }
            if (id.includes('react-router')) {
              return 'router-vendor';
            }
            if (id.includes('axios') || id.includes('dayjs') || id.includes('moment')) {
              return 'utils-vendor';
            }
            if (id.includes('exceljs') || id.includes('xlsx')) {
              return 'excel-vendor';
            }
            return 'vendor';
          }
          // 将页面组件分离到不同的 chunk
          if (id.includes('/pages/')) {
            const pageName = id.split('/pages/')[1].split('/')[0];
            return `page-${pageName}`;
          }
          // 将组件分离到不同的 chunk
          if (id.includes('/components/')) {
            return 'components';
          }
        },
        chunkFileNames: (chunkInfo) => {
          const facadeModuleId = chunkInfo.facadeModuleId ? chunkInfo.facadeModuleId.split('/').pop() : 'chunk';
          return `js/[name]-[hash].js`;
        },
        entryFileNames: 'js/[name]-[hash].js',
        assetFileNames: (assetInfo) => {
          const info = assetInfo.name.split('.');
          const ext = info[info.length - 1];
          if (/\.(css)$/.test(assetInfo.name)) {
            return `css/[name]-[hash].${ext}`;
          }
          if (/\.(png|jpe?g|gif|svg)$/.test(assetInfo.name)) {
            return `images/[name]-[hash].${ext}`;
          }
          return `assets/[name]-[hash].${ext}`;
        },
      },
    },
    // 启用 CSS 代码分割
    cssCodeSplit: true,
    // 设置 chunk 大小警告限制
    chunkSizeWarningLimit: 1000,
  },
  optimizeDeps: {
    include: [
      'react',
      'react-dom',
      'react-router-dom',
      'antd/es/layout',
      'antd/es/menu',
      'antd/es/button',
      'antd/es/input',
      'antd/es/form',
      'antd/es/table',
      'antd/es/modal',
      'antd/es/message',
      'antd/es/notification',
      'antd/es/spin',
      'antd/es/select',
      'antd/es/date-picker',
      'antd/es/tabs',
      'antd/es/card',
      'antd/es/avatar',
      'antd/es/dropdown',
      'antd/es/badge',
      'antd/es/list',
      'antd/es/checkbox',
      'antd/es/radio',
      'antd/es/switch',
      'antd/es/slider',
      'antd/es/progress',
      'antd/es/tooltip',
      'antd/es/popover',
      'antd/es/drawer',
      'antd/es/upload',
      'antd/es/transfer',
      'antd/es/tree',
      'antd/es/tree-select',
      'antd/es/cascader',
      'antd/es/auto-complete',
      'antd/es/mention',
      'antd/es/rate',
      'antd/es/affix',
      'antd/es/anchor',
      'antd/es/back-top',
      'antd/es/breadcrumb',
      'antd/es/calendar',
      'antd/es/carousel',
      'antd/es/collapse',
      'antd/es/descriptions',
      'antd/es/empty',
      'antd/es/image',
      'antd/es/pagination',
      'antd/es/result',
      'antd/es/skeleton',
      'antd/es/statistic',
      'antd/es/steps',
      'antd/es/timeline',
      'antd/es/typography',
      'antd/es/divider',
      'antd/es/space',
      'antd/es/config-provider',
      'antd/es/locale',
      'antd/es/theme',
      '@ant-design/icons',
    ],
  },
  esbuild: {
    treeShaking: true,
    drop: ['console', 'debugger'],
  },
  // 启用 gzip 压缩
  define: {
    __VUE_OPTIONS_API__: true,
    __VUE_PROD_DEVTOOLS__: false,
  },
});